<script type="text/javascript">
$(function(){
	var pg = App.base.serverList.ui, pp = pg.params || {},
		gd = pg.seek('grid'), env = pg.seek('env'),
		envs = Cache.base.env('server');

	env.mac('combo', {
		width: 80,
		boxWidth: 80,
		keyField: 'k',
		displayField: 'v',
		data: envs,
		onSelect: function(me, sk, sd){
			pp.env = sk[0];
			gd.load({ env: pp.env });
		}
	}).val('');
	
	gd.mac('grid', {
		cols : [{
			field: 'ip', title : 'IP地址', width: 120
		},{
			field: 'code', title : '编号 ', width: 100
		},{
			field: 'env', title : '环境 ', width: 100
		},{
			field: 'name', title : '名称 ', width: 210
		},{
			field: 'status', title : '状态', width: 140,
			render: function(r) {
				var s = '<span>' + Cache.base.server.status[r.status] + '</span>';
				var mon = $('<span class="a"></span>').click(function(){
					App.show({
						ctx: 'base',
						action: 'serverMonitor',
						params: { server: r }
					}, $.noop, pg.parent());
				}).append('资源监控');
				return $('<div></div>').append(s).append('&nbsp;').append(mon);
			}
		},{
			field: 'id', title : '操作', width: 200,
			render: function(r) {
				var p = $('<div></div>');
				if (App.logon.power('server.stop')){
					if (r.status == 0) {
						$('<span class="a"></span>').click(function(){
							
						}).append('启用').appendTo(p);
					} else if (r.status == 1) {
						$('<span class="a"></span>').click(function(){
							
						}).append('停用').appendTo(p);
					}
					p.append('&nbsp;');
				}
				if (App.logon.power('server.save')){
					$('<span class="a"></span>').click(function(){
						App.open({
							title:'编辑服务器',
							ctx:'base',
							action:'serverEdit',
							model: true,
							params: {
								projId: pg.params.id,
								id: r.id
							}
						},function(rs){
							//pg.callback(pg.params.id);
							gd.load()
						});
					}).append('修改').appendTo(p);
					p.append('&nbsp;');
				}
				if (App.logon.power('server.delete')){
					$('<span class="a"></span>').click(function(){
						mac.confirm('确定要删除 ' + r.name + ' 服务器？',function(){
							$.post(App.ctx + '/server/delete.do', {
								id: r.id
							},function(rs){
								gd.load();
							});
						});
					}).append('删除').appendTo(p);
				}
				return p;
			}
		}],
		loader: {
			url: App.ctx + '/server/list.do',
			params: {
				pageNo: 1, pageSize: 10,
				projId: pg.params.id
			},
			autoLoad: true
		},
		pagerLength: 10
	});

	if (App.logon.power('server.save')){
		pg.seek('add').click(function(){
			App.open({
				title: '添加服务器',
				ctx: 'base',
				action:'serverEdit',
				model: true,
				params: {
					projId: pp.id,
					env: pp.env
				}
			},function(){
				pg.callback(pg.params.id);
				gd.load()
			});
		}).show();
	}
	
	pg.seek('key').keydown(function(e) {
		if (e.keyCode == 13)
			gd.load({ key: $(this).val() });
	});
	pg.seek('find').click(function(e) {
		gd.load({
			key: pg.seek('key').val(),
			env: env.val()
		});
	});

	function adjust(){
		var h = pg.parent().parent().height();
		pg.height(h);
		gd.parent().height(h-50);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="head">
	<button name="add" class="fl hidden">增加服务器</button>
	<span class="label fl">环境：</span>
	<div class="combo fl" name="env" ></div>
	<span>&nbsp;&nbsp;&nbsp;&nbsp; 关键字：</span>
	<input type="text" name="key" />
	<button name="find">查找</button>
</div>
<div class="body">
	<div class="grid" name="grid"></div>
</div>